package com.xinzi.apartment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzi.apartment.dao.DormitoryInfoDao;
import com.xinzi.apartment.entity.DormitoryInfoEntity;
import com.xinzi.apartment.service.DormitoryInfoService;
import com.xinzi.apartment.common.utils.PageUtils;
import com.xinzi.apartment.common.utils.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("dormitoryInfoService")
public class DormitoryInfoServiceImpl extends ServiceImpl<DormitoryInfoDao, DormitoryInfoEntity> implements DormitoryInfoService {

    private static final String ORDER_FILED_FLAT_NO = "flat_no";
    private static final String ORDER_FILED_REMAIN = "remain";

    @Resource
    private DormitoryInfoDao dormitoryInfoDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<DormitoryInfoEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)) {
            // 有查询条件
            queryWrapper.eq("id", key).or()
                    .eq("max_live_num", key).or()
                    .eq("cost", key).or()
                    .eq("phone", key).or()
                    .eq("flat_no", key).or()
                    .eq("remain", key);
        }

        // 按公寓楼号排序返回
        IPage<DormitoryInfoEntity> page = this.page(
                new Query<DormitoryInfoEntity>().getPage(params, ORDER_FILED_FLAT_NO, true),
                queryWrapper
        );

        return new PageUtils(page);
    }


    /**
     * 查询还有剩余空位的所有寝室
     */
    @Override
    public List<DormitoryInfoEntity> queryHasRemainDormitoriesByGenderFlag(Integer flag) {
        return dormitoryInfoDao.findHasNotDistributionByGenderFlag(flag);
    }

}